package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcArrangeDAO;
import com.trkj.dao.jpa.CcDeductDAO;
import com.trkj.dao.jpa.CcDeductdetailDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.CcPrepaymentDAO;
import com.trkj.dao.jpa.CcScheduledetailDAO;
import com.trkj.dao.jpa.CcSunmoneyDAO;
import com.trkj.dao.jpa.CcSurgeryDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.pojos.CcArrange;
import com.trkj.pojos.CcDeduct;
import com.trkj.pojos.CcDeductdetail;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.CcPrepayment;
import com.trkj.pojos.CcScheduledetail;
import com.trkj.pojos.CcSunmoney;
import com.trkj.pojos.CcSurgery;
import com.trkj.pojos.Tbstaff;
import com.trkj.vo.CcArrangeScheduledetailVo;
import com.trkj.vo.CcArrangeVo;

@Service
@Transactional
public class CcArrangeService {

	@Autowired
	private CcArrangeDAO ccarrangeDAO;
	@Autowired
	private CcPatientDAO ccpDAO;
	@Autowired
	private CcSurgeryDAO csurgeryDAO;
	@Autowired
	private CcPrepaymentDAO prepaymentDAO;
	@Autowired
	private IYzrTbstaffDAO staffDAO;
	@Autowired
	private CcSunmoneyDAO csunmoneyDAO;
	@Autowired
	private CcScheduledetailDAO ccschedtDAO;
	@Autowired
	private CcDeductDAO ccDeductDAO;
	@Autowired
	private CcDeductdetailDAO deductdetailDAO;
	public void calCcArrange(Long[] list){
		for(int i=0;i<list.length;i++){
			CcArrange ca=ccarrangeDAO.findOne(list[i]);
			ca.setAstate("已取消");
			ca.setArrdate(new Date());
			ccarrangeDAO.save(ca);
		}
	}
	public void okCcArrange(Long[] list){
		for(int i=0;i<list.length;i++){
			CcArrange ca=ccarrangeDAO.findOne(list[i]);
			CcSunmoney cs=csunmoneyDAO.findOne(ca.getCcsid().longValue());
			//cs.getSunmoney();
			CcPrepayment prepayment= prepaymentDAO.findBypatient(ca.getAcostid());
			if(prepayment!=null){
				prepayment.setPpmoney(prepayment.getPpmoney().subtract(cs.getSunmoney()));
				prepaymentDAO.save(prepayment);
				CcDeduct cdd=new CcDeduct();
				cdd.setDedate(new Date());
				cdd.setDmoney(cs.getSunmoney());
				cdd.setDpaymentid(new BigDecimal(prepayment.getPpaymentid()));
				ccDeductDAO.save(cdd);
				CcDeductdetail cddd=new CcDeductdetail();
				cddd.setDddate(new Date());
				cddd.setDdeductid(new BigDecimal(cdd.getDeductid()));
				cddd.setDdmoney(cs.getSunmoney());
				cddd.setDdname(cs.getCcsname());
				deductdetailDAO.save(cddd);
			}else{
				CcPrepayment prepayment2=new CcPrepayment();
				prepayment2.setPpatientno(ca.getAcostid());
				prepayment2.setPpmoney(new BigDecimal(0).subtract(cs.getSunmoney()));
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				String dateString = formatter.format(new Date());
				prepayment2.setPpremarks(dateString);
				prepaymentDAO.save(prepayment2);
				CcDeduct cdd=new CcDeduct();
				cdd.setDedate(new Date());
				cdd.setDmoney(cs.getSunmoney());
				cdd.setDpaymentid(new BigDecimal(prepayment2.getPpaymentid()));
				ccDeductDAO.save(cdd);
				CcDeductdetail cddd=new CcDeductdetail();
				cddd.setDddate(new Date());
				cddd.setDdeductid(new BigDecimal(cdd.getDeductid()));
				cddd.setDdmoney(cs.getSunmoney());
				cddd.setDdname(cs.getCcsname());
				deductdetailDAO.save(cddd);
			}
			ca.setAstate("已处理");
			ca.setArrdate(new Date());
			ccarrangeDAO.save(ca);
		}
	}
	public void addCcScheduledetail(CcArrangeScheduledetailVo ccArrsdd){
		CcArrange carr=ccArrsdd.getArrange();
		CcArrange carr2=ccarrangeDAO.findOne(carr.getArrangeid());
		carr.setFixdate(new Date());
		carr.setArrdate(null);
		carr.setAstate("未处理");
		if(carr2!=null){
			carr2.setFixdate(new Date());
			carr2.setArrdate(null);
			carr2.setAstate("未处理");
			carr2.setAcostid(carr.getAcostid());
			carr2.setAdoctorid(carr.getAdoctorid());
			carr2.setArrdoctorrid(carr.getArrdoctorrid());
			carr2.setAsurgeryid(carr.getAsurgeryid());
			carr2.setCcsid(carr.getCcsid());
			//System.err.println(carr2.getAsurgeryid()+"2");
			ccarrangeDAO.save(carr2);
		}else{
			//System.err.println(carr.getAsurgeryid()+"1");
			ccarrangeDAO.save(carr);
		}
		List<CcScheduledetail> list2=ccschedtDAO.queryByschId(new BigDecimal(carr.getArrangeid()));
		List<CcScheduledetail> list=ccArrsdd.getList();
		if(list2.size()>0){
			ccschedtDAO.delete(list2);
		}
		for(CcScheduledetail csdd:list){
			CcScheduledetail csdd2 =new CcScheduledetail();
			csdd2.setSchId(new BigDecimal(carr.getArrangeid()));
			csdd2.setSdnanaid(csdd.getSdnanaid());
			csdd2.setSdnananame(csdd.getSdnananame());
			ccschedtDAO.save(csdd2);
		}
		
	
	}
	public  CcArrange queryOneById(long id){
		return ccarrangeDAO.findOne(id);
	}
	public List<CcScheduledetail> qureyByschId(String schId){
		 return ccschedtDAO.queryByschId(new BigDecimal(schId));
	}
	public List<CcSurgery> queryAllCcSurgery(){
		return (List<CcSurgery>) csurgeryDAO.findAll();
	}
	public List<CcSunmoney> queryAllCcSunmoney(){
		return (List<CcSunmoney>) csunmoneyDAO.findAll();
	}
	public Map<String,Object> findByAllCcArrange(int page,int rows,String pdid){
		Map<String,Object> map =new HashMap<>();
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcArrange>list=ccarrangeDAO.findBypdid( pr,new BigDecimal(pdid),new BigDecimal(pdid));
		map.put("total", ccarrangeDAO.findByConut(new BigDecimal(pdid),new BigDecimal(pdid)));
		map.put("rows", ccArrangeToCcArrangeVo(list));
		return map;
	}
	public List<CcArrangeVo> findByastate(String pdid){
		List<CcArrange>list=ccarrangeDAO.findByastate(new BigDecimal(pdid),new BigDecimal(pdid),"未处理");
		return ccArrangeToCcArrangeVo(list);
	}
	public List<Tbstaff> qureyBypostNo(String postNo){
		 return staffDAO.findBystaffPostNo(new BigDecimal(postNo));
	}
	public List<CcArrangeVo> qureBypatientName(String pname,BigDecimal adoctorid){
		List<CcPatient> listcp =ccpDAO.findBypname(pname);
		List<CcArrangeVo> listVo=new ArrayList<CcArrangeVo>();
		for(CcPatient cp:listcp){
			List<CcArrange> list= ccarrangeDAO.findByacostid(adoctorid, adoctorid, new BigDecimal(cp.getPatientid()));
			listVo=ccArrangeToCcArrangeVo(list);
		}
		return listVo;
	}
	public List<CcArrangeVo> findByadoctoridOrarrd(BigDecimal paid,String name){
		 List<Tbstaff> staff= staffDAO.queryByStaffName(name);
		 List<CcArrangeVo> listVo=new ArrayList<CcArrangeVo>();
		 for(Tbstaff st:staff){
			 List<CcArrange> list= ccarrangeDAO.findByadoctoridOrarrdoctorrid(paid, new BigDecimal(st.getStaffNo()));
			 listVo=ccArrangeToCcArrangeVo(list);
		 }
		return listVo;
	}
	public List<CcArrangeVo> findByadoctoridOrarrd2(BigDecimal paid,String name){
		 List<Tbstaff> staff= staffDAO.queryByStaffName(name);
		 List<CcArrangeVo> listVo=new ArrayList<CcArrangeVo>();
		 for(Tbstaff st:staff){
			 List<CcArrange> list= ccarrangeDAO.findByadoctoridOrarrdoctorrid(new BigDecimal(st.getStaffNo()),paid);
			 listVo=ccArrangeToCcArrangeVo(list);
		 }
		return listVo;
	}
 	public List<CcArrangeVo> ccArrangeToCcArrangeVo(List<CcArrange> list){
		List<CcArrangeVo> listVo=new ArrayList<CcArrangeVo>();
		for(CcArrange ca:list){
			CcArrangeVo caVo=new CcArrangeVo();
			caVo.setAcostid(ca.getAcostid());
			caVo.setAdoctorid(ca.getAdoctorid());
			caVo.setArrangeid(ca.getArrangeid());
			if(ca.getArrdate()!=null){
				caVo.setArrdate(ca.getArrdate());
			}else{
				caVo.setArrdate(null);
			}
			
			caVo.setArrdoctorrid(ca.getArrdoctorrid());
			caVo.setAstate(ca.getAstate());
			caVo.setAsurgeryid(ca.getAsurgeryid());
			caVo.setFixdate(ca.getFixdate());
			
			caVo.setSuntime(ca.getSuntime());
			CcSunmoney csm=csunmoneyDAO.findOne(ca.getCcsid().longValue());
			caVo.setCc_sid(csm.getCcsid());
			caVo.setSunmoney(csm.getSunmoney());
			caVo.setSurname(csm.getCcsname());
			CcPatient cp= ccpDAO.findOne(caVo.getAcostid().longValue());
			caVo.setPid(cp.getPatientid());
			caVo.setPname(cp.getPname());
			CcSurgery cs=csurgeryDAO.findOne(caVo.getAsurgeryid().longValue());
			caVo.setSurgeryid(cs.getSurgeryid());
			caVo.setSaddress(cs.getSaddress());
			Tbstaff sta=staffDAO.findOne(caVo.getAdoctorid().longValue());
			caVo.setAdoctoridname(sta.getStaffName());
			if(caVo.getArrdoctorrid()!=null){
				Tbstaff sta2=staffDAO.findOne(caVo.getArrdoctorrid().longValue());
				caVo.setArrdoctorridname(sta2.getStaffName());
			}
			listVo.add(caVo);
		}
		return listVo;
	}
}
